import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_charge_price_set/views/charge_stage_price_widget.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_charge_price_set/views/charge_stage_price_widgets.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/effective_data_widget.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/effective_date_widgets.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/item_tow_checkbox_widget.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_photovoltaic_price_set/add_photovoltaic_price_set_controller.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/elec_price_title_widget.dart';
import 'package:yt_devops/pages/site/site_setting/site_setting_base_info/widgets/item_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'add_charge_price_set_controller.dart';

class AddChargePriceSetPage extends GetView<AddChargePriceSetController> {
  const AddChargePriceSetPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AddChargePriceSetController>(
        builder: (controller) =>Scaffold(
            backgroundColor: Colour.backgroundColor,
      // resizeToAvoidBottomInset: false,
      appBar: AppBarWidget.withRightActions(ResKey.addChargeStationBilling.tr),
      body:
             Container(
             child: ListView(padding: EdgeInsets.zero, children: [
               Container(
                 height: 10,
                 color: Colour.backgroundColor,
               ),
               Container(
                 clipBehavior: Clip.hardEdge,
                 margin: const EdgeInsets.only(left: 16,right: 16),
                 decoration: BoxDecoration(
                   borderRadius: BorderRadius.circular(8),
                   color: Colors.white,
                 ),
                 child:Column(
                 children: [
                   ElecPriceTitleWidget(controller.titleStringList[0]),
                   ItemWidget(
                     ResKey.siteSettingElectricityRoleName.tr,
                     controller.ruleNameController,
                     unitString: '',
                     titleColor: Colour.otherTextColor,
                     inputTextColor: Colour.mainTextColor,
                     titleFont: 14,
                     isCanEdit: true,
                     textChangeCallBack: (str) {
                       controller.checkCommitStatus();
                     },
                   ),
                   ItemTowCheckboxWidget(
                       ResKey.siteSettingElectricityEffectiveStatus.tr, ResKey.siteSettingEffective.tr, ResKey.siteSettingUnEffective.tr, controller.status, (
                       index) {
                     controller.status = index;
                   })
                 ],
               ),),
               Container(
                 height: 10,
                 color: Colour.backgroundColor,
               ),
               EffectiveDateWidgets(controller.timeDateList, () {
                 controller.updateTimeList();
               }, (index) {
                 controller.deleteTimeList(index);
               }, () {
                 controller.update();
               }),
               Container(
                 height: 10,
                 color: Colour.backgroundColor,
               ),
               ChargeStagePriceWidgets(
                   controller.stagePriceList, () {
                 controller.updateStagePriceList();
               }, (index) {
                 controller.deleteStagePriceList(index);
               }, () {
                 controller.update();
               }, () {
                 controller.update();
               }),
               Container(
                 height: 10,
                 color: Colour.backgroundColor,
               ),

             ])),
            bottomNavigationBar: Container(
              color: Colour.white,
              padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
              child: YtBrnBottomButtonPanel(
                  enableMainButton: controller.isEnableCommit,
                  mainButtonName: ResKey.save.tr,
                  mainButtonOnTap: () => controller.save(),
                  secondaryButtonName: ResKey.cancel.tr,
                  enableSecondaryButton: true,
                  secondaryButtonBorder:
                  Border.all(color: Colour.dividerColor),
                  secondaryButtonOnTap: () =>
                      controller.cancelEdit()),
            )
        ),
    );
  }

  _addEffectiveDataWidgets(BuildContext context) {
    List<Widget> list = [];
    if (controller.timeDateList.length > 1) {
      for (int i = 1; i < controller.timeDateList.length; i++) {
        var widget = EffectiveDataWidget(context, '${ResKey.siteSettingDate.tr}${i + 1}',
          controller.timeDateList[i]['effectiveTime'],
          controller.timeDateList[i]['expirationTime'], (startDateTime,
              endDateTime) {
            controller.timeDateList[i] = {
              'effectiveTime': startDateTime,
              'expirationTime': endDateTime,

            };
            controller.update();
          }, () {
            controller.deleteTimeList(i);
          }, isDelete: true,);
        list.add(widget);
      }
    }
    return SizedBox(
      height: (controller.timeDateList.length - 1) * 55,
      child: Column(
        children: list,
      ),
    );
  }

  _addStagePriceWidgets(BuildContext context) {

    List<Widget> list = [];
    if (controller.stagePriceList.length > 1) {
      for (int i = 1; i < controller.stagePriceList.length; i++) {
        var widget = ChargeStagePriceWidget(
          context,
          ResKey.siteSettingElectricityTimeStage.tr,
          controller.stagePriceList[i]['intervalStartTime'],
          controller.stagePriceList[i]['intervalEndTime'],
          controller.stagePriceList[i]['electricityFees'].toString(),
          controller.stagePriceList[i]['serviceFees'].toString(),
              (startDateTime, endDateTime) {
            var map = controller.stagePriceList[i];
            map['intervalStartTime'] = startDateTime;
            map['intervalEndTime'] = endDateTime;
            controller.stagePriceList[i] = map;
            controller.update();
          },
              () {
            controller.deleteStagePriceList(i);
          },
              (str) {
            var map = controller.stagePriceList[i];
            map['electricityFees'] = str;
            controller.stagePriceList[i] = map;
          }, (str) {
          var map = controller.stagePriceList[i];
          map['serviceFees'] = str;
          controller.stagePriceList[i] = map;
        },
          isDelete: true,);
        list.add(widget);
      }
    }
    return SizedBox(
      height: (controller.stagePriceList.length - 1) * 160,
      child: Column(
        children: list,
      ),
    );
  }

  
}
